package com.shengruan.user.action;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shengruan.admin.jdbc.UserAddressJdbc;
import com.shengruan.admin.vo.UserAddress;
import com.shengruan.common.BaseAction;
import com.shengruan.common.Constants;

public class UserAddressAction extends BaseAction {
    Logger logger = LoggerFactory.getLogger(getClass());

    private UserAddressJdbc userAddressJdbc = (UserAddressJdbc) this.getJdbc(UserAddressJdbc.class);

    public void load() throws Exception {
        UserAddress userAddress = this.userAddressJdbc.getByUserId(this.getCurrentUser().getId());
        if(null == userAddress) {
            userAddress = new UserAddress();
            userAddress.setUserId(this.getCurrentUser().getId());
            userAddress.setRecipientName(this.getCurrentUser().getRealName());
            userAddress.setMobileNumber(this.getCurrentUser().getMobile());
            this.userAddressJdbc.save(userAddress);

            userAddress = this.userAddressJdbc.getByUserId(this.getCurrentUser().getId());
        }

        this.setRequest("userAddress", userAddress);
        this.forward("/user/user-address.jsp");
    }

    public void edit() throws Exception {
        String id = this.input("id");
        String recipientName = this.input("recipientName");
        String province = this.input("province");
        String city = this.input("city");
        String area = this.input("area");
        String streetAddress = this.input("streetAddress");
        String zipCode = this.input("zipCode");
        String phoneNumber = this.input("phoneNumber");
        String mobileNumber = this.input("mobileNumber");

        UserAddress userAddress = this.userAddressJdbc.getById(Integer.valueOf(id));
        userAddress.setRecipientName(recipientName);
        userAddress.setProvince(province);
        userAddress.setCity(city);
        userAddress.setArea(area);
        userAddress.setStreetAddress(streetAddress);
        userAddress.setZipCode(Integer.valueOf(zipCode));
        userAddress.setPhoneNumber(phoneNumber);
        userAddress.setMobileNumber(mobileNumber);

        this.userAddressJdbc.update(userAddress);

        this.setRequest(Constants.MESSAGE, "修改收货地址成功");
        this.setRequest("userAddress", this.userAddressJdbc.getById(Integer.valueOf(id)));
        this.forward("/user/user-address.jsp");
    }
}
